package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjCodeGoodsChangeLog;
import com.zb.mapper.ZjCodeGoodsChangeLogMapper;
import com.zb.result.PageUtil;
import com.zb.service.ViewGoodsService;
import com.zb.service.ZjCodeGoodsChangeLogService;
import com.zb.view.ViewGoods;
import com.zb.vo.CodeGoodsChangeLogListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 编码商品变更记录表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-04
 */
@Service
public class ZjCodeGoodsChangeLogServiceImpl extends ServiceImpl<ZjCodeGoodsChangeLogMapper, ZjCodeGoodsChangeLog> implements ZjCodeGoodsChangeLogService {
    @Resource
    private ZjCodeGoodsChangeLogMapper codeGoodsChangeLogMapper;
    @Resource
    private ViewGoodsService viewGoodsService;


    // 保存编码信息变更记录
    @Override
    public void save(AccounLoginInfo loginInfo, Long codeId, Integer type, Long goodsId, Long goodsSpecId, String remark) {
        ZjCodeGoodsChangeLog log = new ZjCodeGoodsChangeLog();
        log.setId(IdWorker.getId());
        log.setCustAccId(loginInfo.getCustAccId());
        log.setCodeId(codeId);
        log.setType(type);
        log.setGoodsId(goodsId);
        log.setGoodsSpecId(goodsSpecId);
        log.setRemark(remark);
        log.setCreateEmpId(loginInfo.getId());
        log.setCreateEmp(loginInfo.getName());
        this.save(log);
    }

    // 根据编码ID删除编码变更记录
    @Override
    public void deleteByCodeIds(List<Long> codeIdList) {
        if (null == codeIdList || codeIdList.isEmpty()) {
            return;
        }
        codeGoodsChangeLogMapper.deleteByCodeIds(codeIdList);
    }

    // 编码商品变更记录(编码溯源)列表查询接口
    @Override
    public Map<String, Object> getList(Long codeId, PageUtil pageUtil) {
        QueryWrapper<ZjCodeGoodsChangeLog> wrapper = new QueryWrapper<>();
        wrapper.eq("code_id", codeId);
        PageUtil.setPageLimit(pageUtil);
        Page<ZjCodeGoodsChangeLog> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);

        List<Long> goodsIds = page.getRecords().stream().map(ZjCodeGoodsChangeLog::getGoodsId).distinct().toList();
        List<Long> goodsSpecIdss = page.getRecords().stream().map(ZjCodeGoodsChangeLog::getGoodsSpecId).distinct().toList();
        List<ViewGoods> viewGoodsList = viewGoodsService.getByGoodsIdsAndGoodsSpecIds(goodsIds, goodsSpecIdss);
        List<CodeGoodsChangeLogListVo> voList = page.getRecords().stream().map(log -> {
            CodeGoodsChangeLogListVo vo = new CodeGoodsChangeLogListVo();
            BeanUtils.copyProperties(log, vo);
            viewGoodsList.stream().filter(f -> Objects.equals(f.getGoodsType(), log.getType()) && Objects.equals(f.getId(), log.getGoodsId()) && Objects.equals(f.getSpecId(), log.getGoodsSpecId()))
                    .findFirst().ifPresent(specView -> {
                        vo.setBrand(specView.getBrand());
                        vo.setGoodsModel(specView.getModel());
                        vo.setGoodsName(specView.getName());
                        vo.setGoodsSpecName(specView.getSpecName());
                    });
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

}
